package com.bristua.barcode.customer.biz;

import com.bristua.barcode.common.bean.PageBean;
import com.bristua.barcode.customer.dao.TCustomerMapper;
import com.bristua.barcode.customer.model.TCustomer;
import com.bristua.barcode.customer.model.condition.QueryCustomerParams;
import com.bristua.barcode.customer.model.condition.TCustomerExample;
import com.bristua.barcode.customer.model.vo.QueryCustomerVO;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2019/8/6 0006.
 */
@Service
public class QueryCustomerBiz {

    @Resource
    private TCustomerMapper tCustomerMapper;

    public PageBean<TCustomer> queryCustomer(QueryCustomerParams queryCustomerParams){
        TCustomerExample tCustomerExample = new TCustomerExample();
        if(StringUtils.isEmpty(queryCustomerParams.getCustomerName())){
            tCustomerExample.createCriteria().andCustomerNameLike("%"+queryCustomerParams.getCustomerName()+"%");
        }
        tCustomerExample.setOrderByClause("create_time desc");
        Page<TCustomer> objects = PageHelper.startPage(queryCustomerParams.getPageNo(),queryCustomerParams.getPageSize());
        tCustomerMapper.selectByExample(tCustomerExample);
        PageBean tCustomerPageBean = new PageBean<>(objects);
        if(tCustomerPageBean.getList()!=null && tCustomerPageBean.getList().size()>0){
            List<QueryCustomerVO> queryCustomerVOS = new ArrayList<>();
            for(int i=0;i<tCustomerPageBean.getList().size();i++){
                TCustomer tCustomer = (TCustomer)tCustomerPageBean.getList().get(i);
                QueryCustomerVO queryCustomerVO = new QueryCustomerVO();
                BeanUtils.copyProperties(tCustomer,queryCustomerVO);
                queryCustomerVOS.add(queryCustomerVO);
            }
            tCustomerPageBean.getList().clear();
            tCustomerPageBean.getList().addAll(queryCustomerVOS);
        }

        return tCustomerPageBean;
    }
}
